package com.caigou.modules.gallery.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.bean.CommentItemBean
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.gallery.repository.GalleryRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import java.io.File
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/31
 */
@HiltViewModel
class CommentViewModel @Inject constructor(private val repository: GalleryRepository) : ViewModel() {

    val commentDetailLiveData = MutableLiveData<Resource<CommentItemBean>>()
    val replyLiveData = MutableLiveData<Resource<String>>()
    val commentReplyLiveData = MutableLiveData<Resource<String>>()

    fun getCommentDetail(depotId: Int, detailId: Int, type: Int, replyId: Int, userId: String) {
        commentDetailLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getCommentDetail(depotId, detailId, type, replyId, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<CommentItemBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    commentDetailLiveData.value = it
                }
        }
    }

    fun setReply(content: String, files: MutableList<File>, senderId: String, receiverId: String, depotId: Int, detailId: Int, type: Int) {
        replyLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setReply(content, files, senderId, receiverId, depotId, detailId, type)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    replyLiveData.value = it
                }
        }
    }

    fun setCommentReply(
        content: String, files: MutableList<File>, senderId: String,
        receiverId: String, depotId: Int, detailId: Int, type: Int, parentId: Int,
    ) {
        commentReplyLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setCommentReply(content, files, senderId, receiverId, depotId, detailId, type, parentId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    commentReplyLiveData.value = it
                }
        }
    }
}